package action;

import global.Constants;
import global.beanUtils.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.TodoService;
import util.DateUtils;
import util.ServletHelp;
import util.StringUtils;
import domain.Todo;

/**
 * 任务Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class TodoAction extends BaseAction {
	/**
	 * 任务Service
	 * */
	private TodoService todoService = null;

	/**
	 * @param todoService
	 *            the todoService to set
	 */
	public void setTodoService(TodoService todoService) {
		this.todoService = todoService;
	}

	/**
	 * 查询待办事宜列表带分页
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数取得
		Map map = request.getParameterMap();
		Todo paramTodo = new Todo();
		BeanUtils.populate(paramTodo, map);

		// 取得任务列表
		List<Todo> todoList = new ArrayList<Todo>();
		JSONArray datas = new JSONArray();
		todoList = todoService.selectByCriteriaForPaging(paramTodo);
		if(null!=todoList){
			datas = JSONArray.fromObject(todoList);
		}
		int count = todoService.countByCriteria(paramTodo);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存任务
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得参数
		String jsonArr = request.getParameter("jsonArr");
		if (StringUtils.isNotEmpty(jsonArr)) {
			JSONArray todoArr = JSONArray.fromObject(jsonArr);
			for (int i = 0; i < todoArr.size(); i++) {
				JSONObject todoObj = todoArr.getJSONObject(i);
				Todo todo = new Todo();
				todo.setTodoContent(todoObj.getString("todoContent"));
				if(StringUtils.isNotEmpty(todoObj.optString("categoryId"))){
					todo.setCategoryId(todoObj.getInt("categoryId"));
				}else{
					todo.setCategoryId(0);
				}
				if(StringUtils.isNotEmpty(todoObj.optString("dealDateStr"))){
					todo.setDealDate(DateUtils.formatStr2Date(todoObj.getString("dealDateStr"),DateUtils.DATE_PATTON_1));
				}else{
					todo.setDealDate(DateUtils.formatStr2Date(Constants.MAX_DATE,DateUtils.DATE_PATTON_1));
				}
				todo.setLevel(todoObj.getString("level"));
				todo.setStatus(Constants.STATUS_1);
				todo.setDelflag(Constants.DEL_FLAG_1);
				if (StringUtils.isNotEmpty(todoObj.getString("todoId"))) {
					todo.setTodoId(Integer.parseInt(todoObj.getString("todoId")));
					// 更新任务
					todoService.update(todo);
				} else {
					// 添加任务
					todoService.insert(todo);
				}
			}
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 更新任务
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 任务ID
		String todoId = request.getParameter("todoId");

		if(StringUtils.isNotEmpty(todoId)){
			// 参数设置
			Map map = request.getParameterMap();
			Todo todo = new Todo();
			BeanUtils.populate(todo, map);
			// 更新待办事宜
			todoService.update(todo);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 删除任务
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 任务ID
		String todoIdStr = request.getParameter("todoIds");
		// 删除任务
		if (StringUtils.isNotEmpty(todoIdStr)) {
			todoService.delete(todoIdStr);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 完成任务
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward finish(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 任务ID
		String todoIdStr = request.getParameter("todoIds");
		// 完成标识
		String delflag = request.getParameter("delflag");
		// 完成任务
		if (StringUtils.isNotEmpty(todoIdStr)) {
			String[] todoIds = todoIdStr.split(",");
			for (int i = 0; i < todoIds.length; i++) {
				Todo paramTodo = new Todo();
				paramTodo.setTodoId(Integer.parseInt(todoIds[i]));
				paramTodo.setDelflag(delflag);
				// 完成任务
				todoService.update(paramTodo);
			}
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
